﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class ProductDetailBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public ProductDetailBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(T_PRODUCT_DETAIL Detail,out string ID)
        {
            //T_PRODUCT_DETAIL detail = DAO.T_PRODUCT_DETAILs.SingleOrDefault(t => t.BAR_CODE == Detail.BAR_CODE
            //                                && t.BRAND == Detail.BRAND
            //                                && t.LOT_NUMBER == Detail.LOT_NUMBER
            //                                && t.MODEL == Detail.MODEL
            //                                && t.ORIGIN == Detail.ORIGIN
            //                                && t.PACKET_NO == Detail.PACKET_NO
            //                                && t.PRODUCT_ID.ToString() == Detail.PRODUCT_ID.ToString()
            //                                && t.PRODUCTED_DATE == Detail.PRODUCTED_DATE
            //                                && t.SIZE == Detail.SIZE
            //                                && t.STANDARD == Detail.STANDARD
            //                                && t.STOCK_TYPE == Detail.STOCK_TYPE
            //                                && t.SUB_LOT_NUMBER == Detail.SUB_LOT_NUMBER
            //                                && t.InStock_Date==Detail.InStock_Date
            //                                && t.ClientID.ToString()==Detail.ClientID.ToString()
            //                                && t.KeyStandard == Detail.KeyStandard
            //                                && t.SupplyLotNumber == Detail.SupplyLotNumber
            //                                && t.BigClass == Detail.BigClass);

            var detail = DAO.T_PRODUCT_DETAILs.Where(t =>
                                           (string.IsNullOrEmpty(Detail.BAR_CODE) || (!string.IsNullOrEmpty(Detail.BAR_CODE) && (t.BAR_CODE == Detail.BAR_CODE)))
                                            && ((t.BRAND==null&&Detail.BRAND==null)|| t.BRAND == Detail.BRAND)
                                            && ( (t.LOT_NUMBER==null&&Detail.LOT_NUMBER==null)||t.LOT_NUMBER == Detail.LOT_NUMBER)
                                            && ((t.MODEL==null&&Detail.MODEL==null)||t.MODEL == Detail.MODEL)
                                            && ((t.ORIGIN==null&&Detail.ORIGIN==null)||t.ORIGIN == Detail.ORIGIN)
                                            && ((t.PACKET_NO==null&&Detail.PACKET_NO==null)||t.PACKET_NO == Detail.PACKET_NO)
                                            && t.PRODUCT_ID.ToString() == Detail.PRODUCT_ID.ToString()
                                            && ((t.PRODUCTED_DATE==null&&Detail.PRODUCTED_DATE==null)||(t.PRODUCTED_DATE == Detail.PRODUCTED_DATE))
                                            && ((t.SIZE==null&&Detail.SIZE==null)||t.SIZE == Detail.SIZE)
                                            && ((t.STANDARD==null&&Detail.STANDARD==null)||t.STANDARD == Detail.STANDARD)
                                            && ((t.STOCK_TYPE==null&&Detail.STOCK_TYPE==null)||(t.STOCK_TYPE == Detail.STOCK_TYPE))
                                            && ((t.SUB_LOT_NUMBER==null&&Detail.SUB_LOT_NUMBER==null)||t.SUB_LOT_NUMBER == Detail.SUB_LOT_NUMBER)
                                            && ((t.InStock_Date==null&&Detail.InStock_Date==null)||(t.InStock_Date == Detail.InStock_Date))
                                            && ((t.ClientID==null&&Detail.ClientID==null)||t.ClientID.ToString() == Detail.ClientID.ToString())
                                            && ((t.KeyStandard==null&&Detail.KeyStandard==null)||t.KeyStandard == Detail.KeyStandard)
                                            && ((t.SupplyLotNumber==null&&Detail.SupplyLotNumber==null)||t.SupplyLotNumber == Detail.SupplyLotNumber)
                                            && ((t.BigClass==null&&Detail.BigClass==null)||t.BigClass == Detail.BigClass));
            if (detail != null&&detail.Count()>0)
            {
                ID = detail.FirstOrDefault().ID.ToString();
                return true;
            }
            else
            {
                ID = "";
                return false;
            }
        }

        public IEnumerable<T_PRODUCT_DETAIL> GetProductDetailList()
        {
            var list = DAO.T_PRODUCT_DETAILs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_PRODUCT_DETAIL GetProductDetailByID(string ID)
        {
            return DAO.T_PRODUCT_DETAILs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddProductDetail(T_PRODUCT_DETAIL ProductDetail)
        {
            try
            {
                ProductDetail.ID = DAO.GetGUID();

                DAO.T_PRODUCT_DETAILs.InsertOnSubmit(ProductDetail);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddProductDetail(T_PRODUCT_DETAIL ProductDetail, out string ProductDetailGuid)
        {
            try
            {
                ProductDetail.ID = DAO.GetGUID();
                
                DAO.T_PRODUCT_DETAILs.InsertOnSubmit(ProductDetail);
                DAO.SubmitChanges();

                ProductDetailGuid = ProductDetail.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                ProductDetailGuid = "";
                return -1;
            }
        }

        public int UpdateProductDetail(T_PRODUCT_DETAIL ProductDetail)
        {
            try
            {
                T_PRODUCT_DETAIL updateEntity = DAO.T_PRODUCT_DETAILs.SingleOrDefault(t=>t.ID.ToString()==ProductDetail.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.BAR_CODE = ProductDetail.BAR_CODE;
                updateEntity.BRAND = ProductDetail.BRAND;
                updateEntity.LOT_NUMBER = ProductDetail.LOT_NUMBER;
                updateEntity.MODEL = ProductDetail.MODEL;
                updateEntity.ORIGIN = ProductDetail.ORIGIN;
                updateEntity.PACKET_NO = ProductDetail.PACKET_NO;
                updateEntity.PRODUCT_ID = ProductDetail.PRODUCT_ID;
                updateEntity.PRODUCTED_DATE = ProductDetail.PRODUCTED_DATE;
                updateEntity.SIZE = ProductDetail.SIZE;
                updateEntity.STANDARD = ProductDetail.STANDARD;
                updateEntity.STOCK_TYPE = ProductDetail.STOCK_TYPE;
                updateEntity.SUB_LOT_NUMBER = ProductDetail.SUB_LOT_NUMBER;
                updateEntity.KeyStandard = ProductDetail.KeyStandard;
                updateEntity.SupplyLotNumber = ProductDetail.SupplyLotNumber;
                updateEntity.BigClass = ProductDetail.BigClass;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteProductDetail(string ID)
        {
            try
            {
                T_PRODUCT_DETAIL deleteEntity = DAO.T_PRODUCT_DETAILs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_PRODUCT_DETAILs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
